package com.flow.demo.module.gd.controller.admin.address;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.flow.demo.framework.common.pojo.PageParam;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.util.object.BeanUtils;
import static com.flow.demo.framework.common.pojo.CommonResult.success;

import com.flow.demo.framework.excel.core.util.ExcelUtils;

import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import static com.flow.demo.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.flow.demo.module.gd.controller.admin.address.vo.*;
import com.flow.demo.module.gd.dal.dataobject.address.AddressDO;
import com.flow.demo.module.gd.service.address.AddressService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 收获地址")
@RestController
@RequestMapping("/gd/address")
@Validated
public class AddressController {

    @Resource
    private AddressService addressService;

    @PostMapping("/create")
    @Operation(summary = "创建收获地址")
    @PreAuthorize("@ss.hasPermission('gd:address:create')")
    public CommonResult<Integer> createAddress(@Valid @RequestBody AddressSaveReqVO createReqVO) {
        return success(addressService.createAddress(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收获地址")
    @PreAuthorize("@ss.hasPermission('gd:address:update')")
    public CommonResult<Boolean> updateAddress(@Valid @RequestBody AddressSaveReqVO updateReqVO) {
        addressService.updateAddress(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收获地址")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gd:address:delete')")
    public CommonResult<Boolean> deleteAddress(@RequestParam("id") Integer id) {
        addressService.deleteAddress(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收获地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gd:address:query')")
    public CommonResult<AddressRespVO> getAddress(@RequestParam("id") Integer id) {
        AddressDO address = addressService.getAddress(id);
        return success(BeanUtils.toBean(address, AddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收获地址分页")
    @PreAuthorize("@ss.hasPermission('gd:address:query')")
    public CommonResult<PageResult<AddressRespVO>> getAddressPage(@Valid AddressPageReqVO pageReqVO) {
        PageResult<AddressDO> pageResult = addressService.getAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收获地址 Excel")
    @PreAuthorize("@ss.hasPermission('gd:address:export')")
    @OperateLog(type = EXPORT)
    public void exportAddressExcel(@Valid AddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AddressDO> list = addressService.getAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收获地址.xls", "数据", AddressRespVO.class,
                        BeanUtils.toBean(list, AddressRespVO.class));
    }

}